
package com.huawei.common.okhttp;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 功能描述
 *
 * @author cWX1142527
 * @since 2023年01月18日
 */
public class Java_1081_不同字符的最小子序列 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            Deque<Character> stack = new ArrayDeque<>();
            char[] chars = s.toCharArray();

            // 统计字符的最大下标
            int[] maxIndex = new int[26];
            for (int i = 0; i < chars.length; i++) {
                maxIndex[chars[i] - 'a'] = i;
            }
            List<Character> exitStackVal = new ArrayList<>();
            for (int j = 0; j < chars.length; j++) {
                Character curVal = chars[j];
                Character topVal;
                while (!stack.isEmpty()) {
                    topVal = stack.peek();
                    // 栈里 i元素大于后面 i+1的元素 同时 i元素后续还有出现的话,将 栈顶元素弹出，放入 i+1的元素
                    if (topVal == curVal) {
                        break;
                    }
                    if (topVal > curVal) {
                        // top的最大下标大于当前位置时，说明后面还有数据
                        if (maxIndex[topVal - 'a'] > j) {
                            stack.pop();
                            exitStackVal.remove(topVal);
                        } else {
                            // 已经存在栈中不入栈
                            if (exitStackVal.contains(curVal)) {
                                break;
                            }
                            stack.push(curVal);
                            exitStackVal.add(curVal);
                            break;
                        }
                    } else {
                        // 已经存在栈中不入栈
                        if (exitStackVal.contains(curVal)) {
                            break;
                        }
                        stack.push(curVal);
                        exitStackVal.add(curVal);
                    }
                }
                if (stack.isEmpty()) {
                    stack.push(curVal);
                    exitStackVal.add(curVal);
                }
            }
            StringBuilder out = new StringBuilder();
            while (!stack.isEmpty()) {
                Character val = stack.pop();
                out.append(val);
            }
            System.out.println(out.reverse());
        }
    }
}
